package vn.cent.games.findnumber.main;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import vn.cent.games.findnumber.beans.GameBean;
import vn.cent.games.findnumber.utils.Commands;

import com.smartfoxserver.v2.core.SFSEventType;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.smartfoxserver.v2.extensions.SFSExtension;

/**
 * Main extension class
 * 
 * @author TV.Vinh
 * @version 1.0
 */

public class FindNumberExtension extends SFSExtension {

	/** current games */
	private ConcurrentHashMap<Integer, GameBean> games = null;

	@Override
	public void init() {

		// Initialize games list
		games = new ConcurrentHashMap<Integer, GameBean>();

		// REGISTER REQUEST HANDLERS
		addRequestHandler(Commands.CMD_READY, ReadyHandler.class);
//		addRequestHandler(Commands.CMD_MOVE, MoveHandler.class);

		// REGISTER EVENT HANDLERS
//		addEventHandler(SFSEventType.USER_JOIN_ROOM,
//				UserJoinedEventHandler.class);
		addEventHandler(SFSEventType.USER_LEAVE_ROOM,
				UserLeftEventHandler.class);
		addEventHandler(SFSEventType.USER_DISCONNECT,
				UserDisconnectedEventHandler.class);
	}

	@Override
	public void destroy() {

		super.destroy();

//		removeEventHandler(SFSEventType.USER_JOIN_ROOM);
		removeEventHandler(SFSEventType.USER_LEAVE_ROOM);
		removeEventHandler(SFSEventType.USER_DISCONNECT);

		removeRequestHandler(Commands.CMD_READY);
		removeRequestHandler(Commands.CMD_MOVE);
	}

	// GETTER & SETTER

	public ConcurrentHashMap<Integer, GameBean> getGames() {
		return games;
	}

	/**
	 * notify players game will start in 5 seconds
	 * 
	 * @param players
	 *            List of recipients
	 */
	public void getReady(List<User> players) {
		trace("list size: " + players.size());
		ISFSObject resObj = new SFSObject();
		resObj.putUtfString("opn", players.get(1).getName());

		// notify player that game was started
		send(Commands.CMD_getREADY, resObj, players.get(0));

		resObj.putUtfString("opn", players.get(0).getName());
		send(Commands.CMD_getREADY, resObj, players.get(1));

//		trace("--------players get ready--------");
	}

	/**
	 * Send start game invocation
	 * 
	 * @param players
	 *            List of recipients
	 */
	public void startGame(List<User> players) {
		// TODO WARNING NULL DATA

		for (User user : players) {
			// if one of players left game, cancel invoke
			if (!user.isPlayer())
				return;
		}

//		ISFSObject resObj = new SFSObject();
		send(Commands.CMD_GO, null, players);

//		trace("--------game started--------");
	}

	/**
	 * game end
	 * 
	 * @param players
	 *            : list of players
	 * @param winner
	 *            (1: p1 win | 2: p2 win | 0: draw)
	 */
	public void announceWinner(List<User> players) {
		// TODO WARNING NULL DATA
//		ISFSObject resObj = new SFSObject();
		
		send(Commands.CMD_END, null, players);
//		trace("--------game ended--------");
	}
}
